<?php
include("../db_connect.php");
?>


<script>
var limiter = 0;
$(document).ready(function () {

$("#accordion_auto").accordion({heightStyle: "content",collapsible: true});
$("#auto_cars").html(loader).load("admin/view_auto_data.php");

$("#report_auto").click(function (){
var brand2 = $("#addbrand");
//alert(brand2.val());
window.open("reports/auto_reports.php?brand="+brand2.val());
});

$("#addbrand").change(function () {
		model = $("#src_model").val();
		brand = $(this).attr("value");
		var model2="model";
		if(brand == ""){
			brand = "";
		}
		if(model == ""){
			model = "";
		}
		
		$("#add_model").empty().load("profile/selection.php?command="+model2 +"&modelid="+brand);
		$("#auto_cars").html(loader).load("admin/view_auto_data.php?brand="+brand+"&model="+model);
});

$("#add_model").change(function () {
		brand = $("#src_model").val();
		model = trim($(this).attr("value"));
		if(model == ""){
			model=="";
		}
	$("#auto_cars").html(loader).load("admin/view_auto_data.php?brand="+brand+"&model="+model);

});



$("#addpictures").click(function () {
		if(limiter <=6){
		
		var a = '</br> <div><img id="img_prev'+limiter+'" src="#"/></div><input type="file" name="image[]" id="image" onchange="readURL(this);"></br>'
		
		$("#addmorepicture").append(a);
		
		}
		else{
		alert("you can only upload 6 picture");
		}
		return false;
		
	});
$("#abrand").change(function () {
		var id = $(this).attr("value");
		var model="model";
		$("#amodel").empty().load("admin/selection.php?command="+model +"&modelid="+id);
	
	});
	
$("#registerButton").click(function () {

var opttype = $("#types");
var optbrand = 	$("#abrand");
var model = $("#amodel");
var name = $("#name");
var description = $("#description");
var condition = $("#conditions");
var fuel = $("#fuel");
var transmission = $("#transmission");
var year = $("#year"); 
var price = $("#price");
var quantity = $("#quantity");

	
	if(opttype.val() ==  "Select" || opttype.val() == "0"){
	alert("Select a type");
	opttype.focus();
	return false;
	}
	else if(optbrand.val() ==  "Select" || optbrand.val() ==  "0"){
	alert("Select a brand");
	optbrand.focus();
	return false;
	}
	else if(model.val() ==  "Select" || model.val() ==  ""){
	alert("Select a model");
	model.focus();
	return false;
	}
	else if(name.val() ==  "" || name.val() ==  " "){
	alert("Please input a name");
	name.focus();
	return false;
	}
	else if(description.val() ==  "" || description.val() ==  " "){
	alert("Please input your description");
	description.focus();
	return false;
	}
	else if(condition.val() ==  "Select Condition" || condition.val() ==  "0"){
	alert("Select a condition");
	condition.focus();
	return false;
	}
	else if(fuel.val() ==  "Select Fuel" || fuel.val() ==  "0"){
	alert("Select a fuel type");
	fuel.focus();
	return false;
	}
	else if(transmission.val() ==  "Select Transmission"|| transmission.val() ==  "0"){
	alert("Select transmission");
	transmission.focus();
	return false;
	}
	else if(year.val() ==  "Select" || year.val() ==  "0"){
	alert("Select year");
	year.focus;
	return false;
	}
	else if(price.val() ==  "" || price.val() == " "){
	alert("Please input quantity");
	price.focus();
	return false;
	}
	else if(isNaN(price.val())){
	alert("Price only accept No.");
	price.focus();
	return false;
	}
	else if(quantity.val() ==  ""|| quantity.val() ==  " "){
	alert("Please input quantity");
	quantity.focus();
	return false;
	}
	else if(isNaN(quantity.val())){
	alert("Price only quantity No.");
	quantity.focus();
	return false;
	}
	else{
	 return true;
	}
	

});	

});
function trim(data){
	var a = new Array();
	a = data.split(" ");
	var x = a.length;
	
	if(x>1){
		
		return a;
	}
	else{
		
		return data;
	}
	
	
}
function readURL(input) {
	var b = '#img_prev'+limiter;
	
	if (input.files && input.files[0]) {
		var reader = new FileReader();
		reader.onload = function (e) {
			$(b).attr('src', e.target.result).width(300).height(120);
			limiter++;
		};
			reader.readAsDataURL(input.files[0]);
	}
}
	
function autodelete(value){

if(confirm("Do you want to delete this?")){
$.ajax({
	
	url:"admin/modifyData.php?category=auto&action=delete&pid="+value,
	success:function(data){
		if(data){
		alert("Successful Delete");
		$("#auto_cars").html(loader).load("admin/viewAuto.php");
		}
		else{
			alert("Unsuccessful Delete");
		}
		
	}


});
}



}
function autoEdit2(value){
	$("#edit_car").load("admin/editmsgbox.php?pid="+value);

}

</script>
<style>

h4 a{
font-size:12px;
}
#auto_cars tr{ 
	display: table;            /* this makes borders/margins work */
	border: 1px solid black;
	width:100%;
	margin: 2px 0px 0px 0px;
}
</style>
<div id="msgbox"></div>
<div id="accordion_auto">
	<h4 style="color:white">Add New Car</h4>
<div>
		<div id="auto" class="auto">

			<div id="wrapper">
					<div id="steps">
						<form id="formElem" name="formElem" action="admin/autodata.php" method="post" enctype="multipart/form-data">

							<fieldset class="step">
								<legend>Item Name</legend>
								<p>
									<label for="types">Type</label>
									<select id="types" value="types"  name="types">
									<?php 
										$sql = mysql_query("SELECT * FROM tbltypes");
										$opt2 = '<option id="opttype" name="none" value="0">Select</option> ';
										
										while($row = mysql_fetch_array($sql) ){
											$opt2 .= '<option id="opttype" value="'.$row["types"].'" >'.$row["types"].'</option>';
										}
										echo $opt2;
									?>
									</select>
									
									
								</p>
								<p>
									<label for="brand">Brand</label>
									<select id="abrand" value="brand" name="brand">
									<?php 
										$sql = mysql_query("SELECT * FROM tblbrand");
										$opt = '<option id="optbrand" name="none" value="0">Select</option> ';
										while($row = mysql_fetch_array($sql) ){
											$opt .= '<option id="optbrand" name="'.$row["brand"].'" value="'.$row["id"].'" >'.$row["brand"].'</option>';
										}
										echo $opt;
									?>
									</select>
								</p>
								<p>
									<label for="Model">Model</label>
									<select id="amodel" value="model" name="model">
									
									</select>
									
								</p>
							
								<p>
									<label for="Name">Name</label>
									<input type="text" id="name" name="name" AUTOCOMPLETE=OFF />
								   
								</p>
							</fieldset>
							
							
							<fieldset class="step">
								<legend>Description</legend>
								<p>
									<label for="description">Description</label>
									<textarea style="width:590px; height: 123px;" id="description" value="description" name="description"></textarea>
								</p>
								
								<p>
									<label for="Conditions">Condition</label>
									 <select id="conditions" value="conditions" name="conditions">
										<option  value="0" >Select Condition</option>
										<option  value="BrandNew" >BrandNew</option>
										<option  value="2ndHand">2ndHand</option>
									</select>
								</p>
								<p>
								  <label for="fuel" id = "fuel">Fuel</label>
									<select id = "fuel" name="fuel">
										<option value="0">Select Fuel</option>
										<option value="Gasoline">Gasoline</option>
										<option value="Diesel">Diesel</option>
										<option value="Lpg">LPG</option>
										<option value="Hybrid">Hybrid</option>
										<option value="Electric">Electric</option>
									</select>
								</p>
								
							</fieldset>
							
							<fieldset class="step">
								<legend>Description</legend>
								<p>
								 <label for="transmission" id = "transmission">Transmission</label>
									<select id = "transmission" name="transmission">
										<option value="0">Select Transmission</option>
										<option value="Manual">Manual</option>
										<option value="Automatic">Automatic</option>
									</select>
								</p>
								<p>
									<label for="year">Year</label>
									<select id="year" name="year">
										<option value="0">Select</option>
										<option>2006</option>
										<option>2007</option>
										<option>2008</option>
										<option>2009</option>
										<option>2010</option>
										<option>2011</option>
										<option>2012</option>
									</select>
								</p>
								<p>
									<label for="price">Price</label>
									<input id="price" name="price" type="number" AUTOCOMPLETE=OFF />
								</p>
								<p>
									<label for="quantity">Quantity</label>
									<input id="quantity" name="quantity" type="number" AUTOCOMPLETE=OFF />
								</p>
								
							</fieldset>
							
							<fieldset class="step">
							<p>
								<legend>Color</legend>	
										<label for="color"></label>
										<select id="color" name="color">
											<option value="">Select</option>
											<?php 
												$sql = mysql_query("SELECT * FROM tblcolor")or die(mysql_error());
												while($row = mysql_fetch_array($sql)){
													echo '<option value="'.$row['color'].'">'.$row['color'].'</option>';
												}
											
											?>
										
										</select>
										</p>
								<p>
									<legend>Payment</legend>
									  <label for="payment" ></label>
										<select id = "modeofpayment" name="modeofpayment">
											<option value="0">Select Payment</option>
											<option value="Cash">Cash</option>
											<option value="Installment">Installment</option>
											
										</select>
								</p>
								
								</fieldset>
							<fieldset class="step">
								<legend>Image</legend>
								<div id="addmorepicture">
									
								</div>
								<p>
									<button id="addpictures">Add picture</button>
								</p>
							</fieldset>
							<fieldset class="step">
								<legend>Confirm</legend>
								<p>
									Please fill up all the missing fields before you proceed
								</p>
								<p class="submit">
									<button id="registerButton" type="submit" name = "submit">Add New Auto</button>
								</p>
							</fieldset>
						</form>
					</div>
					
				</div>

		</div>
	
</div>
<h4>View Cars</h4>


<div id="edit_car">
	<div id="searchbox" align="left">
	<!--search div-->
	<div>
		<div class="headersearch ">
		<label>Brand:</label>
		<select id="addbrand" value="brand" name="brand">				
			<?php 
				
				$sql = mysql_query("SELECT * FROM tblbrand");
				$opt = '<option id="addoptbrand" name="none" value="">Select All</option> ';
				while($row = mysql_fetch_array($sql) ){
					$opt .= '<option id="optbrand" name="'.$row["brand"].'" value="'.$row["id"].'" >'.trim($row["brand"]).'</option>';
				}
				echo $opt;
			?>				
		</select>
		
		<label>Model:</label>
		<select id="add_model" value=""></select>
			<button id="report_auto">Report</button>
		</div>
		
	</div>			
			
	<!--end search div-->		
	</div>
	<div id="auto_cars" style="overflow:auto">
				
	</div>
</div>


</div>

							